package org.scboy.dbms.dubbo;

import java.util.ArrayList;
import java.util.List;

import org.apache.dubbo.config.annotation.DubboService;
import org.scboy.coobi.sql.Coobi;
import org.scboy.coobi.sql.factory.EntityFactory;
import org.scboy.dbms.api.IDbManager;
import org.scboy.dbms.api.entity.DbIndex;
import org.scboy.dbms.api.entity.DbTable;
import org.scboy.dbms.api.entity.SQLEntity;
import org.scboy.dbms.api.entity.SqlConfig;
import org.scboy.dbms.dao.DbColumnDao;
import org.scboy.dbms.dao.DbIndexDao;
import org.scboy.dbms.dao.DbTableDao;
import org.scboy.dbms.dao.SqlConfigDao;
import org.scboy.dbms.dao.SqlEntityDao;
import org.scboy.utils.entitys.coobi.Model;
import org.springframework.beans.factory.annotation.Autowired;

import cn.hutool.core.lang.UUID;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;


@DubboService
public class DbManagerImpl implements IDbManager {

	Log log=LogFactory.get();
	
	@Autowired
	Coobi coobi;
	
	@Autowired
	EntityFactory factory;
	
	@Override
	public void initSql(List<SqlConfig> sqls) {

		SqlConfigDao sqlConfigDao = factory.getDao(SqlConfigDao.class);

		SqlEntityDao sqlEntityDao = factory.getDao(SqlEntityDao.class);
		
		//检查是否存在
		for (SqlConfig sqlConfig : sqls) {
			
			Model config=sqlConfigDao.get(Model.create().add("sqlId", sqlConfig.getSqlId()).add("serverName", sqlConfig.getServerName()));
			if(config==null) {
				
				//新增SQL配置
				config=new Model();
				config.add("id", UUID.randomUUID().toString());
				config.add("sqlId", sqlConfig.getSqlId());
				config.add("name", sqlConfig.getName());
				config.add("note", sqlConfig.getNote());
				config.add("serverName", sqlConfig.getServerName());
				config.add("activeVersion", sqlConfig.getSqlEntity().getVersion());
				sqlConfigDao.insert(config);
				
				//新增SQL配置明细
				Model sqlEntity=Model.create();
				sqlEntity.add("id", UUID.randomUUID().toString());
				sqlEntity.add("configId", config.getString("id"));
				sqlEntity.add("name", sqlConfig.getSqlEntity().getName());
				sqlEntity.add("note", sqlConfig.getSqlEntity().getNote());
				sqlEntity.add("entSql", sqlConfig.getSqlEntity().getSql());
				sqlEntity.add("entVersion", sqlConfig.getSqlEntity().getVersion());
				sqlEntityDao.insert(sqlEntity);
			}
			
			if(config.getInt("activeVersion") < sqlConfig.getSqlEntity().getVersion()) {
				
				//数据库版本小于当前版本，进行更新
				config.add("sqlId", sqlConfig.getSqlId());
				config.add("name", sqlConfig.getName());
				config.add("note", sqlConfig.getNote());
				config.add("serverName", sqlConfig.getServerName());
				config.add("activeVersion", sqlConfig.getSqlEntity().getVersion());
				sqlConfigDao.update(config);
				
				//新增SQL配置明细
				Model sqlEntity=Model.create();
				sqlEntity.add("id", UUID.randomUUID().toString());
				sqlEntity.add("configId", config.getString("id"));
				sqlEntity.add("name", sqlConfig.getSqlEntity().getName());
				sqlEntity.add("note", sqlConfig.getSqlEntity().getNote());
				sqlEntity.add("entSql", sqlConfig.getSqlEntity().getSql());
				sqlEntity.add("entVersion", sqlConfig.getSqlEntity().getVersion());
				sqlEntityDao.insert(sqlEntity);
			}
		}
	}

	@Override
	public List<DbTable> taskDbEntity(String serverName) {

		DbTableDao tableDao = factory.getDao(DbTableDao.class);

		DbColumnDao columnDao = factory.getDao(DbColumnDao.class);
		
		DbIndexDao indexDao = factory.getDao(DbIndexDao.class);
		
	 	List<Model> tables= tableDao.select(Model.create().add("serverName", serverName));
	 	
	 	List<DbTable> dbTables=new ArrayList<DbTable>();
	 	
	 	for (Model model : tables) {
			DbTable dbTable=new DbTable();
			dbTable.setComment(model.getString("tabComment"));
			dbTable.setId(model.getString("id"));
			dbTable.setName(model.getString("name"));
			dbTable.setServerName(model.getString("serverName"));
			dbTable.setVersion(model.getInt("tabVersion"));
			
			List<Model> columnModels=columnDao.select(Model.create().add("tableId", model.getString("id")));
			
			List<org.scboy.dbms.api.entity.DbColumn> columns=new ArrayList<org.scboy.dbms.api.entity.DbColumn>();
			
			for (Model column : columnModels) {
				org.scboy.dbms.api.entity.DbColumn dbColumn=new org.scboy.dbms.api.entity.DbColumn();
				dbColumn.setComment(column.getString("colComment"));
				dbColumn.setDbType(column.getString("dbType"));
				dbColumn.setEntityName(column.getString("entityName"));
				dbColumn.setId(column.getString("id"));
				dbColumn.setLength(column.getInt("colLength"));
				dbColumn.setName(column.getString("name"));
				dbColumn.setNotNull(column.getBoolean("notNull"));
				dbColumn.setPrimary(column.getBoolean("colPrimary"));
				dbColumn.setTableId(column.getString("tableId"));
				columns.add(dbColumn);
			}
			
			dbTable.setColumns(columns);
			

			List<DbIndex> indexs=new ArrayList<DbIndex>();

			List<Model> indexModels=indexDao.select(Model.create().add("tableId", model.getString("id")));
			
			for (Model index : indexModels) {
				DbIndex dbIndex=new DbIndex();
				dbIndex.setColumns(index.getString("idxColumns"));
				dbIndex.setId(index.getString("id"));
				dbIndex.setIndexName(index.getString("idxName"));
				dbIndex.setTableId(index.getString("tableId"));
				indexs.add(dbIndex);
			}
			
			dbTable.setIndexs(indexs);
			
			dbTables.add(dbTable);
		}
	 	return dbTables;
	}

	@Override
	public List<SqlConfig> taskSql(String serverName) {

		SqlConfigDao sqlConfigDao = factory.getDao(SqlConfigDao.class);

		SqlEntityDao sqlEntityDao = factory.getDao(SqlEntityDao.class);
		
		List<Model> configs=sqlConfigDao.select(Model.create().add("serverName", serverName));
		
		List<SqlConfig> sqlConfigs=new ArrayList<SqlConfig>();
		
		for (Model model : configs) {
			SqlConfig sqlConfig=new SqlConfig();
			sqlConfig.setActiveVersion(model.getInt("activeVersion"));
			sqlConfig.setId(model.getString("id"));
			sqlConfig.setName(model.getString("name"));
			sqlConfig.setNote(model.getString("note"));
			sqlConfig.setServerName(model.getString("serverName"));
			sqlConfig.setSqlId(model.getString("sqlId"));
			
			Model entity=sqlEntityDao.get(Model.create().add("configId", model.getString("id")).add("activeVersion", model.getInt("activeVersion")));
			
			SQLEntity sqlEntity=new SQLEntity();
			sqlEntity.setConfigId(entity.getString("configId"));
			sqlEntity.setId(entity.getString("id"));
			sqlEntity.setName(entity.getString("name"));
			sqlEntity.setNote(entity.getString("note"));
			sqlEntity.setSql(entity.getString("entSql"));
			sqlEntity.setVersion(entity.getInt("entVersion"));
			
			sqlConfig.setSqlEntity(sqlEntity);
			
			sqlConfigs.add(sqlConfig);
		}
		
		return sqlConfigs;
	}

	@Override
	public int insertTable(DbTable table) {
		DbTableDao dao = factory.getDao(DbTableDao.class);
		Model params=Model.create();
		params.add("id", table.getId());
		params.add("name", table.getName());
		params.add("tabComment", table.getComment());
		params.add("serverName", table.getServerName());
		params.add("tabVersion", table.getVersion());
		return dao.insert(params);
	}

	@Override
	public int updateTable(DbTable table) {
		DbTableDao dao = factory.getDao(DbTableDao.class);
		Model params=Model.create();
		params.add("id", table.getId());
		params.add("name", table.getName());
		params.add("tabComment", table.getComment());
		params.add("serverName", table.getServerName());
		params.add("tabVersion", table.getVersion());
		return dao.update(params);
	}

	@Override
	public int insertColumn(org.scboy.dbms.api.entity.DbColumn column) {
		DbColumnDao dao = factory.getDao(DbColumnDao.class);
		Model params=Model.create();
		params.add("id", column.getId());
		params.add("name", column.getName());
		params.add("colComment", column.getComment());
		params.add("dbType", column.getDbType());
		params.add("entityName", column.getEntityName());
		params.add("tableId",column.getTableId());
		params.add("colLength", column.getLength());
		params.add("notNull", column.isNotNull());
		params.add("colPrimary", column.isPrimary());
		return dao.insert(params);
	}

	@Override
	public int deleteColumns(String tableId) {
		DbColumnDao dao = factory.getDao(DbColumnDao.class);
		return dao.delete(Model.create().add("tableId", tableId));
	}

	@Override
	public int insertIndex(org.scboy.dbms.api.entity.DbIndex index) {
		DbIndexDao dao = factory.getDao(DbIndexDao.class);
		Model params=Model.create();
		params.add("id", index.getId());
		params.add("tableId",index.getTableId());
		params.add("idxName", index.getIndexName());
		params.add("idxColumns", index.getColumns());
		return dao.insert(params);
	}

	@Override
	public int deleteIndexs(String tableId) {
		DbIndexDao dao = factory.getDao(DbIndexDao.class);
		return dao.delete(Model.create().add("tableId", tableId));
	}

	@Override
	public DbTable getTable(String serverName, String tableName) {
		DbTableDao dao = factory.getDao(DbTableDao.class);
		Model model = dao.get(Model.create().add("serverName", serverName).add("name", tableName));
		DbTable dbTable=null;
		if(model!=null) {
			dbTable=new DbTable();
			dbTable.setComment(model.getString("tabComment"));
			dbTable.setId(model.getString("id"));
			dbTable.setName(model.getString("name"));
			dbTable.setServerName(model.getString("serverName"));
			dbTable.setVersion(model.getInt("tabVersion"));
		}
		return dbTable;
	}
}
